package com.netnovelstore.util;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
@Slf4j
public class novelReader {
    //绝对路径
    //例如 /Users/tao/Desktop/java/Servlet/NetNovelStore/src/main/webapp/novelTXT/大奉打更人/1.txt
//    返回content
    static BufferedReader in = null;
    /*
        path => '/Users/tao/Desktop/java/springboot-NetNovelStore/target/classes/static/novel'
        bookName => '大奉打更人'
        number => '1'
        return 'content'
     */
    public static  String readContent(String path,String bookPath,Integer number) throws IOException {
        String filePath = String.format("%s/%s/%s.txt",path,bookPath, number);
        log.info("阅读: "+filePath);
        in = new BufferedReader(new FileReader(filePath));
        return in.readLine();

    }

    /*
    parentsFolderBookPath => ' /Users/tao/Desktop/java/springboot-NetNovelStore/target/classes/static/novel/撒野/'


     */
    public static int getChapterTotal(String realPath, String novelName){

        int l=1,r=Integer.MAX_VALUE,mid;
        while(l<=r){
            mid=l+((r-l)>>1);
            String targetFilePath = String.format("%s/%s/%d.txt",realPath,novelName,mid);
            File file = new File(targetFilePath);
            if(file.exists()){
                l=mid+1;
            }else{
                r=mid-1;
            }

        }
        return r;
    }


}
